import Vue from 'vue'
export default function (errorType, tip, options) {
  let {vnode, query} = options
  if (query) {
    let item = Vue.prototype._vaildationMap.get(query)
    if (!item.dom) {
      let rect = vnode.elm.getBoundingClientRect()
      let errorDiv = document.createElement('div')
      Object.assign(item, {'dom': errorDiv})
      errorDiv.className = 'vaildation-error'
      errorDiv.innerText = messages[errorType](tip)
      errorDiv.style.top = `${rect.top - 35}px`
      errorDiv.style.left = `${rect.left - 35}px`
      document.body.appendChild(errorDiv)
      setTimeout(() => {
        if (item.dom) {
          document.body.removeChild(errorDiv)
          delete item.dom
        }
      }, 2000)
    }
  } else {
    options.setAttribute('placeholder', messages[errorType](tip))
  }
}
const messages = {
  phone: () => '请输入正确的手机号',
  noPhone: () => '不能使用手机号',
  tel: () => '请输入正确的联系方式',
  special: () => '不能包含特殊字符串',
  password: () => '至少一个字母和一个数字',
  number: () => '只能使用数字',
  email: () => `必须是有效的邮箱`,
  ip: (ip) => ` ${ip} 必须是一个有效的地址`,
  date: (date) => ` ${date} 必须是1990-01-1的格式`,
  min: (length) => `至少有 ${length} 位字符`,
  max: (length) => `不能大于${length}位字符`,
  minNum: (length) => `最小${length}`,
  maxNum: (length) => `最大${length}`,
  money: () => `请输入正确的金钱格式`,
  moneywithminus: () => `请输入正确的金钱格式`,
  required: () => `必须填写`,
  select: () => `必须选择`,
  url: (url) => ` ${url}不是有效的url`,
  day: () => '请输入正确的天数',
  area: () => '请选择地址'
}
